using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using MrDev.Graphics;
using MrDev.GameMath;
using MrDev.Physics;

namespace MrDev.GameObjects
{
    /// <summary>
    /// Our fearless adventurer!
    /// </summary>
    class Explosion : GameObject
    {
        private Animation animation;
        private float delay;
        private bool animationStarted = false;
        /// <summary>
        /// Constructors a new player.
        /// </summary>
        public Explosion(Level level, Vector2 position, float delay)
            : base(level)
        {
            this.delay = delay;
            Parameters = PhysicsParameters.CreateExplosionParameters();
            LoadContent();
            Parameters.RelativeCollisionRect = new Rectangle(12, 0, 24, 48);
            Reset(position);
        }

      

        /// <summary>
        /// Loads the player sprite sheet and sounds.
        /// </summary>
        public void LoadContent()
        {
            // Load animated textures.
            animation = new Animation(Level.Content.Load<Texture2D>("Sprites/Effects/explosion"), 0.05f, false, 0,9);
            
        }

        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (delay < 0)
            {
                if (!animationStarted)
                {
                    Level.SoundFXExplode(); 
                    PlayAnimation(animation);
                }
                animationStarted = true;

            }
            else
            {
                delay -= elapsed;
            }
            if (animationStarted && AnimationDone)
                IsAlive = false;
        }


        /// <summary>
        /// Resets the player to life.
        /// </summary>
        /// <param name="position">The position to come to life at.</param>
        public void Reset(Vector2 position)
        {
            Level.Physics.SetObjectPosition(this, position);
            SetColor(Color.White);
            IsAlive = true;
        }
    }
}
